<template>
  <div class="comment-container">
     <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>评论管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-table
        :data="articleList"
        style="width: 100%"
        stripe
        class="table-list"
        >
        <el-table-column
          prop="title"
          label="标题"
          >
        </el-table-column>
        <el-table-column
          prop="total_comment_count"
          label="总评论数"
          >
        </el-table-column>
        <el-table-column
          prop="fans_comment_count"
          label="粉丝评论数">
        </el-table-column>
         <el-table-column
          prop="comment_status"
          label="评论状态"
          >
          <template slot-scope="scope">
            {{scope.row.comment_status ? '正常':'关闭'}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-switch
              :disabled="scope.row.statusDisabled"
              v-model="scope.row.comment_status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeStatus(scope.row)"
              >
            </el-switch>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <!--
         current-page:控制激活页码初始为1
         page-sizes：控制可选的每页大小
         page-size：
        -->
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-sizes="[10, 20, 30]"
          :page-size.sync="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageTotal">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getArticles, commentsStatus } from '@/api/article.js'
export default {
  name: 'commentIndex',
  components: {},
  props: {},
  data () {
    return {
      articleList: [],
      currentPage: 1,
      pageTotal: 0,
      pageSize: 10
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadComment()
  },
  mounted () {},
  methods: {
    loadComment (page = 1) {
      // 让分页激活的页面跟请求数据的页码保持一致
      this.currentPage = page
      getArticles({
        response_type: 'comment',
        page,
        per_page: this.pageSize
      }).then(res => {
        // console.log(res)
        const { results } = res.data.data
        results.forEach(article => {
          article.statusDisabled = false
        })
        this.articleList = results
        this.pageTotal = res.data.data.total_count
      })
    },
    // 每页大小改变
    handleSizeChange (page) {
      // console.log(page)
      this.loadComment(1)
    },
    handleCurrentChange (page) {
      // 加载指定页码数据
      this.loadComment(page)
    },
    changeStatus (article) {
      // console.log(article)
      article.statusDisabled = true
      commentsStatus(article.id, article.comment_status).then(res => {
        // console.log(res)
        article.statusDisabled = false
        this.$message({
          message: article.comment_status ? '文章评论状态开启成功' : '文章评论状态关闭成功',
          type: 'success'
        })
      })
    }
  }
}
</script>

<style scoped lang="less">
 .table-list{
   margin-bottom:30px;
 }
</style>
